<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="//at.alicdn.com/t/c/font_3788257_bqyfiblub3f.css">
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    .star{
      color: gold;
    }
  </style>
</head>
<body>
  <div id="app">
    {{score}}
    <star :score="score" @chage="setScore"></star>
  </div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
  Vue.component('star',{
    props:{
      count:{
        type:Number,
        default:5
      },
      score:{
        type:Number,
        default:0
      }
    },
    template:`
      <div>
        <i class="iconfont star " 
        v-for="item in count" 
        :class="item <= score ? 'icon-pingfen_xuanzhong' : 'icon-pingfen_moren'"
        @mouseenter="updataScore(item)"
        @mouseleave="updataScore(bac_score)"
        @click="bac_score = item"
    ></i>
      </div>
    `,
    data(){
      return {
        bac_score:0
      }
    },
    methods:{
      updataScore(val){
        this.$emit('chage',val)
      }
    },
    created(){
      if(this.score > this.count) throw 'score 不能大于 count'
      this.bac_score = this.score
    }
  })
  new Vue({
    el:'#app',
    data() {
      return {
        count:5,
        score:2,
        bac_score:2
      }
    },
    methods:{
      setScore(val){
        this.score = val
      }
    }
  })
</script>
</html>